<?php
	$imagePath = $_GET['image'];
	$width = '';
	$height = '';
	
	if(isset($_GET['width'])) {
		$width = "_w".$_GET['width'];
	}
	if(isset($_GET['height'])) {
		$height = "_h".$_GET['height'];
	}
	$ext = substr($imagePath, strrpos($imagePath, '.')+1);
	$file = substr($imagePath, 0, strlen($imagePath)-(strlen($ext)+1));
	$filePath = $file.$width.$height.'.'.$ext;
	if(file_exists($filePath)) {
		//die("file exists".$filePath);
		$data	= file_get_contents($filePath);
		// Display resized image
		if($ext == "jpg" || $ext == "jpeg"){
			header('Content-type: image/jpeg');
		}
		elseif($ext == "gif"){
			header('Content-type: image/gif');
		}
		elseif($ext == "png"){
			header('Content-type: image/png');
		}
		echo $data;
		die();
	}
	elseif(isset($_GET['image']) && trim($imagePath) != '' && file_exists($imagePath)) {
		
		$imageCheck = strtolower($imagePath);
		$type = "";
		
		if(strpos($imagePath,".jpg") !== false || strpos($imagePath,".jpeg") !== false) {
			$image = imagecreatefromjpeg($imagePath);
			$type = "jpg";
		}
		elseif(strpos($imagePath,".gif") !== false) {
			$image = imagecreatefromgif($imagePath);
			$type = "gif";
		}
		elseif(strpos($imagePath,".png") !== false) {
			$image = imagecreatefrompng($imagePath);
			$type = "png";
		}
		
		list($width,$height)=getimagesize($imagePath);

		$new_width = 0;		
		$new_height = 0;
		
		// if limit is set it won't allow images to be scaled up to become pixelated, they will output at original size.
		//if(isset($_GET['limit'])) {
			if(isset($_GET['width'])) {
				if($_GET['width'] > ($width)) {
					unset($_GET['width']);
				}
			}		
			elseif(isset($_GET['height'])) {
				if($_GET['height'] > ($height)) {
					unset($_GET['height']);
				}
			}	
		//}	

		// New width and height with appropriate aspect ratio
		if(isset($_GET['width'])) {
			$new_width = $_GET['width'];
			$aspect = $height/$width;
			$new_height = $new_width*$aspect;
		}
		elseif(isset($_GET['height'])) {
			$new_height = $_GET['height'];
			$aspect = $width/$height;
			$new_width = $new_height*$aspect;
		}
		
		// If width or height isn't set use original image size.
		if($new_width == 0 && $new_height == 0) {
			$new_width = $width;		
			$new_height = $height;
		}
		
		// Resample
		$image_resized = imagecreatetruecolor($new_width, $new_height);

		if($type == "png" || $type == "gif") {
			$background = imagecolorallocate($image_resized, 0,0,0);		
			imagecolortransparent($image_resized, $background);
			imagealphablending($image_resized, false);
		}
		
		imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		
		// Display resized image
		if($type == "jpg"){
			header('Content-type: image/jpeg');
			imagejpeg($image_resized,$filePath,33);
			imagejpeg($image_resized,'',33);
		}
		elseif($type == "gif"){
			header('Content-type: image/gif');
			imagegif($image_resized,$filePath);
			imagegif($image_resized);
		}
		elseif($type == "png"){
			header('Content-type: image/png');
			imagepng($image_resized,$filePath);
			imagepng($image_resized);
		}
		
		imagedestroy($image);
		imagedestroy($image_resized);
		die();		
	}
	// If not a valid image path output the ""no image" image.
	else {
			$new_width = 50;
			$new_height = 50;
		
		if(isset($_GET['width'])) {
			$new_width = $_GET['width'];
			$new_height = $_GET['width'];
		}
		elseif(isset($_GET['height'])) {
			$new_height = $_GET['height'];
			$new_width = $_GET['height'];
		}
		
		$image = imagecreatefromjpeg("images/noimage.jpg");
		list($width,$height)=getimagesize("images/noimage.jpg");
		
		$image_resized = imagecreatetruecolor($new_width, $new_height);
		imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

		header('Content-type: image/jpeg');
		imagejpeg($image_resized);
		
		imagedestroy($image);
		imagedestroy($image_resized);
		die();		
		
	}
?>
